package com.leetcode;

/**
 * 150. 逆波兰表达式求值
 * 数组模拟栈
 *
 * @author fy
 * @date 2022/4/12 17:43
 */
public class Solution150_1 {

    public int evalRPN(String[] tokens) {
        if (tokens == null || tokens.length == 0) {
            return 0;
        }
        int n = tokens.length;
        int[] stack = new int[(n + 1)/2];
        int index = -1;
        for (String token : tokens) {
            switch (token) {
                case "+":
                    index--;
                    stack[index] += stack[index + 1];
                    break;
                case "-":
                    index--;
                    stack[index] -= stack[index + 1];
                    break;
                case "*":
                    index--;
                    stack[index] *= stack[index + 1];
                    break;
                case "/":
                    index--;
                    stack[index] /= stack[index + 1];
                    break;
                default:
                    stack[++index] = Integer.valueOf(token);
                    break;
            }
        }
        return stack[index];
    }

}
